package com.lagou.esweb.common;

import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;

import static com.lagou.esweb.common.constant.RespEnum.ERROR;
import static com.lagou.esweb.common.constant.RespEnum.OK;

@Data
public final class Response<T> implements Serializable {

    private Integer status;

    private String message;

    private T data;

    public static Response OK() {
        return OKWithMessage(OK.getMessage());
    }

    public static <T> Response<T> OK(T data) {
        return OK(OK.getMessage(), data);
    }


    public static Response OKWithMessage(String message) {
        return OK(message, null);
    }

    public static <T> Response<T> OK(String message, T data) {
        Response response = new Response();
        response.setStatus(OK.getCode());
        response.setData(data);
        response.setMessage(message);
        return response;
    }

    public static Response FAIL(String message) {
        Response response = new Response();
        response.setStatus(ERROR.getCode());
        response.setMessage(StringUtils.isBlank(message) ? ERROR.getMessage() : message);
        return response;
    }

    public static Response FAIL() {
        return FAIL(ERROR.getMessage());
    }

}
